﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RBOSS.Business.Products;
using RBOSS.Business.CustomerModule;


namespace RBOSS.WebUI
{

    public partial class ProductUI : System.Web.UI.Page
    {
        private readonly CustomerSubSystem _customerSubSystem = new CustomerSubSystem();

        public int ProductId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            var productId = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(productId))
            {
                ProductId = int.Parse(productId);
            }
            else
            {
                ProductId = 0;
            }
            if (!Page.IsPostBack)
            {
                FillCategoryItems();
                
                if (productId != null)
                {
                    
                    GetProductItemInfo(ProductId);
                    btnInsert.Text = "Update";
                }
                else
                {
                    btnInsert.Text = "Insert";
                }
            }
            
            var email = (string)Session["email"];
            var role = (string)Session["role"];
            if (Session["email"] == null || (string)Session["role"] != "Vendor")
                Response.Redirect("Login.aspx");

            btnInsert.Visible = true;
           
            
        }

        private void GetProductItemInfo(int nProductId)
        {
            ProductSubsystem objProdSubSystem = new ProductSubsystem();
            var ProductItem = objProdSubSystem.GetProduct(nProductId);

            txtProdName.Text = ProductItem.ProductName;
            txtPrice.Text = ProductItem.Price.ToString();
            txtQuantity.Text = ProductItem.Qunatity.ToString();
            txtDescription.Text = ProductItem.Description;
            ddlCategory.SelectedValue = ProductItem.CategoryId.ToString();
            chkProductAvailable.Checked = ProductItem.IsProductAvailable == 1 ? true : false;
        }

        private void FillCategoryItems()
        {
            var allCategories = _customerSubSystem.GetAllCategories();
            ddlCategory.DataSource = allCategories;
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "CategoryId";
            ddlCategory.DataBind();
        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(fileupload.PostedFile.FileName))
            {
                var path = Server.MapPath("~/ProductImages/");
                fileupload.SaveAs(path + fileupload.PostedFile.FileName);
            }
            ProductItem objProdItem = new ProductItem();
            var customerId = _customerSubSystem.GetCustomerId((string)Session["email"]);
            objProdItem.ProductId = ProductId;
            objProdItem.ProductName = txtProdName.Text;
            objProdItem.Qunatity    = int.Parse(txtQuantity.Text);
            objProdItem.Price       = decimal.Parse(txtPrice.Text);
            objProdItem.Image = ("/ProductImages/" + fileupload.PostedFile.FileName);
            objProdItem.Description = txtDescription.Text;
            objProdItem.UserId = customerId;
            objProdItem.IsProductAvailable = chkProductAvailable.Checked == true ? 1: 0;
            objProdItem.CategoryId = Convert.ToInt32(ddlCategory.SelectedValue);              


            
            ProductSubsystem ps = new ProductSubsystem();
            if(ProductId != 0)
            {
                ps.UpdateProduct(objProdItem);
            }
            else
            {
                ps.CreateProduct(objProdItem);
            }
                     
            Response.Redirect("VendorProductsUI.aspx");
        }

 
        protected void btnImgageBrowse_Click(object sender, EventArgs e)
        {

        }
    }
}